from datetime import datetime
import pandas as pd

def get_weather_data(district_name, target_date):
    """获取指定地区在特定日期的气象数据"""
    # 读取该地区的所有气象数据
    pressure = pd.read_csv(f'./data/pressure/{district_name}.csv')
    rain = pd.read_csv(f'./data/rain/{district_name}.csv')
    rainfall_probability = pd.read_csv(f'./data/rainfall_probability/{district_name}.csv')
    temperature = pd.read_csv(f'./data/temperature/{district_name}.csv')
    wind_speed = pd.read_csv(f'./data/wind_speed/{district_name}.csv')

    # 处理数据格式
    for df in [pressure, rain, rainfall_probability, temperature, wind_speed]:
        df.columns = ['date', 'value']  # 重命名列
        df['date'] = pd.to_datetime(df['date'])  # 转换为datetime

    # 查询指定日期的数据
    pressure_value = pressure[pressure['date'] == target_date]['value'].values[0]
    rain_value = rain[rain['date'] == target_date]['value'].values[0]
    rainfall_prob_value = rainfall_probability[rainfall_probability['date'] == target_date]['value'].values[0]
    temp_value = temperature[temperature['date'] == target_date]['value'].values[0]
    wind_value = wind_speed[wind_speed['date'] == target_date]['value'].values[0]

    return {
        '气压': pressure_value,
        '降雨量': rain_value,
        '降雨概率': rainfall_prob_value,
        '温度': temp_value,
        '风速': wind_value
    }

# 将日期字符串转换为datetime对象
date_str = '2025-5-01'
date = datetime.strptime(date_str, '%Y-%m-%d').date()
target_date = pd.to_datetime(date)

# 定义需要处理的地区列表
districts = ['元朗区', '北区', '大埔区','屯门区','西贡区']

# 创建各气象条件的列表
pressure_list = []
rain_list = []
rainfall_probability_list = []
temperature_list = []
wind_speed_list = []

# 遍历每个地区并获取气象数据
for district in districts:
    try:
        weather_data = get_weather_data(district, target_date)
        # 将各气象数据添加到对应列表（转换为原生Python浮点数）
        pressure_list.append(float(weather_data['气压']))
        rain_list.append(float(weather_data['降雨量']))
        rainfall_probability_list.append(float(weather_data['降雨概率']))
        temperature_list.append(float(weather_data['温度']))
        wind_speed_list.append(float(weather_data['风速']))

        print(f"\n{district} 在 {date} 的气象数据:")
        for key, value in weather_data.items():
            print(f"{key}: {value}")
    except Exception as e:
        print(f"获取 {district} 的数据时出错: {e}")
print(pressure_list)
print(rain_list)
print(rainfall_probability_list)
print(temperature_list)
print(wind_speed_list)





